package com.yc.service;

import com.google.gson.Gson;
import com.yc.domain.MessageBean;

import com.yc.controllers.websocket.EmailWebsocket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zwei
 * @create 2024-08-11 19:07
 */
@Component
@Slf4j
@Transactional
public class JmsMessageConsumer {
    @Autowired
    private EmailBiz mailBiz;
    @Autowired
    private VelocityTemplateBizImpl velocityTemplateBiz;
    @Autowired
    private EmailWebsocket emailWebsocket;

    @JmsListener(destination = "银行信息变更")    //监听的队列  当有消息发送到这个队列  就会执行这个方法
    public void receiveMessage(String message) {
        log.info("用户接收到的信息:" + message);
        Gson gson = new Gson();
        MessageBean mb = gson.fromJson(message, MessageBean.class);
        //产生要发生的邮件内容
        String context = velocityTemplateBiz.getEmailContext(mb.getOpType(), mb.getAccount(), mb.getMoney(), mb.getToaccountid());
        log.info("发送的内容:" + context);
        try {
            mailBiz.sendMail(mb.getEmail(), "账户变动通知", context);
            emailWebsocket.send(message);     //发送websocket消息
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
    }
}
